package com.example.budget3.recurring;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.List;

import com.example.budget3.R;
import com.example.budget3.data.RecurrTransactionData;
import com.example.budget3.util.UtilMethods;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class RecurrListAdapter extends ArrayAdapter<RecurrTransactionData> {

	private int resource;
	private List<RecurrTransactionData> values;
	private LayoutInflater mInflater;
	private Context context;
	private ViewHolder holder;
	private int catCol;
	
	public static final String KEY_LIST_PREFERENCE = "pref_currency";
	
	public RecurrListAdapter(Context context, int resource,
			List<RecurrTransactionData> objects) {
		super(context, resource, objects);
		this.resource = resource;
		this.values = objects;
		this.context = context;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return values.size();
	}

	@Override
	public RecurrTransactionData getItem(int position) {
		return values.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("SimpleDateFormat")
	@Override
	public View getView(final int position, View convertView2, ViewGroup parent) {

		final RecurrTransactionData data = values.get(position);

		if (convertView2 == null) {
			convertView2 = mInflater.inflate(resource, parent, false);
			holder = new ViewHolder();

			holder.name = (TextView) convertView2.findViewById(R.id.row_name);
			holder.time = (TextView) convertView2.findViewById(R.id.row_date);
			holder.cost = (TextView) convertView2.findViewById(R.id.row_cost);
			holder.category = (TextView) convertView2.findViewById(R.id.row_category);
			holder.periodData = (TextView) convertView2.findViewById(R.id.period_data);
			holder.push = (LinearLayout) convertView2.findViewById(R.id.expandable_toggle_button);
			convertView2.setTag(holder);
		} else {
			holder = (ViewHolder) convertView2.getTag();
		}

		int colour;
		if (data.isType()) {

			holder.push.setGravity(Gravity.RIGHT);
			colour = context.getResources().getColor(R.color.expense);
			holder.category.setText(data.getExpenseCategory().toString());
			switch (data.getExpenseCategory()) {
				case Travel:
					catCol = context.getResources().getColor(R.color.travel); 
					break;
				case Leisure:
					catCol = context.getResources().getColor(R.color.leisure); 
					break;
				case Other:
					catCol = context.getResources().getColor(R.color.other); 
					break;
				case Bills:
					catCol = context.getResources().getColor(R.color.bills);
					break;
				case Food:
					catCol = context.getResources().getColor(R.color.food);
					break;
				case Utilities:
					catCol = context.getResources().getColor(R.color.utilities);
					break;
				case Rent:
					catCol = context.getResources().getColor(R.color.rent);
					break;
				case Shopping:
					catCol = context.getResources().getColor(R.color.shopping);
					break;
			}
			holder.category.setTextColor(catCol);
		} else {
			holder.push.setGravity(Gravity.LEFT);
			colour = context.getResources().getColor(R.color.income);
			holder.category.setText(data.getIncomeCategory().toString());
			switch (data.getIncomeCategory()) {
				case Bonus:
					catCol = context.getResources().getColor(R.color.bonus); 
					break;
				case Gift:
					catCol = context.getResources().getColor(R.color.gift); 
					break;
				case Other:
					catCol = context.getResources().getColor(R.color.other); 
					break;
				case Transfer:
					catCol = context.getResources().getColor(R.color.transfer);
					break;
				case Wage:
					catCol = context.getResources().getColor(R.color.wage);
					break;
			}
			holder.category.setTextColor(catCol);
		}

		holder.cost.setText(UtilMethods.getCostToString(context, data.getCost()));
		holder.cost.setTextColor(colour);

		Calendar startTime  = Calendar.getInstance();
		startTime.setTime(data.getNextTimeDate());
		holder.time.setText(formatPeriod(data));
		holder.periodData.setText(UtilMethods.calendarToString(startTime));
		holder.name.setText(data.getName());
		holder.name.setTextColor(colour);
		
		return convertView2;

	}

	private String formatPeriod(RecurrTransactionData data) {
		switch (data.getPeriod()) {
		case 0:
			DecimalFormat df = new DecimalFormat("00");
			return "Daily at " + df.format(data.getHour()) + ":"+ df.format(data.getMinute());
		case 1:
			String[] days = new String[]{"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday", "Sunday"};
			return "Weekly on " +days[ data.getDayOfWeek()-2];
		case 2:
			return "Monthly on the " + getDayOfMonthSuffix(data.getDayOfMonth());
		default:
			return "";
		}
		
	}
	
	public static String getDayOfMonthSuffix(final int day) {
		
		String result = day +"";
		
		if (day >= 11 && day <= 13) {
			result += "th";
        }else{
	        switch (day % 10) {
	            case 1:  result += "st"; break;
	            case 2:  result += "nd"; break;
	            case 3:  result += "rd"; break;
	            default: result += "th"; break;
	        }
        }
		
		if(day >= 29){
			result += " or last day";
		}
		return result;
    }

	static class ViewHolder {
		public TextView periodData;
		public LinearLayout push;
		public TextView category;
		public TextView cost;
		public TextView time;
		public TextView name;
		public TextView type;
	}

	public void setData(List<RecurrTransactionData> data) {
		this.values = data;
	}

}